package com.prs.crm.action.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerDegree;
import com.prs.crm.service.crm.CustomerDegreeService;

public class CustomerDegreeAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private CustomerDegree  customerDegree;
	private CustomerDegreeService customerDegreeService;
	private Integer[] degreeIds;
	
	@Action("listCustomerDegree")
	public String listCustomerDegree() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerDegree.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerDegreeService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editCustomerDegree")
	public String editCustomerDegree() {
		if(degreeIds!=null){
			this.customerDegree=this.customerDegreeService.get(degreeIds[0]);
		}
		return SUCCESS;
	}
	@Action(value = "saveCustomerDegree", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerDegree"),
			@Result(name = "error", type = "chain", location = "editCustomerDegree")})
	public String saveCustomerDegree(){
		if(customerDegree.getId()==null){
			this.customerDegreeService.save(customerDegree);
		}else{
			this.customerDegreeService.submit(customerDegree);
		}
		return SUCCESS;
	}
	@Action(value = "savebtmDegree", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerDegree"),
			@Result(name = "error", type = "chain", location = "listCustomerDegree")})
	public String savebtmDegree(){
		this.customerDegree=this.customerDegreeService.get(degreeIds[0]);
		customerDegree.setRemark("默认");
		this.customerDegreeService.submit(customerDegree);
		return SUCCESS;
	}
	@Action(value = "savebtqDegree", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerDegree"),
			@Result(name = "error", type = "chain", location = "listCustomerDegree")})
	public String savebtqDegree(){
		List<CustomerDegree> list=this.customerDegreeService.getAllDegree();
		for(int i=0;i<list.size();i++){
			CustomerDegree d=list.get(i);
			if(d.getRemark()!=null){
				d.setRemark("");
				this.customerDegreeService.submit(d);
			}
		}
		return SUCCESS;
	}
	@Action(value = "removeCustomerDegree", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerDegree"),
			@Result(name = "error", type = "chain", location = "listCustomerDegree")})
	public String removeCustomerContract(){
		this.customerDegreeService.batchRemove(degreeIds);
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public CustomerDegree getCustomerDegree() {
		return customerDegree;
	}
	public void setCustomerDegree(CustomerDegree customerDegree) {
		this.customerDegree = customerDegree;
	}
	public CustomerDegreeService getCustomerDegreeService() {
		return customerDegreeService;
	}
	public void setCustomerDegreeService(CustomerDegreeService customerDegreeService) {
		this.customerDegreeService = customerDegreeService;
	}

	public Integer[] getDegreeIds() {
		return degreeIds;
	}

	public void setDegreeIds(Integer[] degreeIds) {
		this.degreeIds = degreeIds;
	}
	
	
}
